// packageI/invoice/invoice-details/invoice-details.js
const app = getApp();
Page({
  data: {
    invoice_id: "",
    language: '',
    detailData: {},
    invoiceType: "",
    orderGoods: [],
    steps: ["订单提交", "订单完成", "开票完成"],
    stepIndex: 1,

    show: false,
    email: "",
  },

  onLoad: function (options) {
    if (options.tag) {
      this.setData({
        tag: options.tag
      })
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    if (options.invoice_id) {
      this.setData({
        invoice_id: options.invoice_id
      });
    }
    this.getInvoiceDetail();
  },

  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

  getInvoiceDetail() {
    let urlStr = app.getNetAddresss("plugin.invoice.frontend.index.details");
    let json = {}
    if (this.data.tag == 'balance_invoice') {
      urlStr = app.getNetAddresss("plugin.balance-invoice.frontend.balance-invoice.detail");
      json.rid = this.data.id
    } else {
      json.id = this.data.invoice_id
    }
    app._getNetWork({
      url: urlStr,
      data: json,
      success: resdata => {
        let res = resdata.data;
        if (res.result !== 1) {
          return app.tips(res.msg);
        }
        if (this.data.tag == 'balance_invoice') {
          this.setData({
            invoiceDetails: res.data
          })
          return
        }
        let steps = [];
        if (res.data.event == 1) {
          steps = ["订单提交", "订单完成", "开票完成"];
        } else {
          steps = ["订单提交", "订单付款", "开票完成"];
        }
        this.setData({
          detailData: res.data,
          invoiceType: res.data.invoice_type,
          orderData: res.data.has_one_order,
          orderGoods: res.data.has_one_order?.has_many_order_goods,
          stepIndex: res.data.order_invoice_status,
          steps
        });
      }
    })
  },

  showEditEmail() {
    this.setData({
      show: true
    })
  },

  hideEditEmail() {
    this.setData({
      show: false,
      email: "",
    })
  },

  changeEmail(e) {
    this.setData({
      email: e.detail.value
    })
  },
  enterFun(e) {
    if (app._isTextEmpty(this.data.email)) {
      return app.tips("发送邮箱不能为空")
    }
    let urlStr = app.getNetAddresss("plugin.invoice.frontend.index.go-email");
    let {
      invoice_pdf = '', invoice_sn
    } = this.data.detailData;
    let json = {};
    if (this.data.tag == 'balance_invoice') {
      json = { 
        email: this.data.email, 
        invoice_sn: this.data.invoiceDetails.invoice_no,
        invoice_type:'balance' 
      }
    } else {
      json = {
        email: this.data.email,
        invoice_sn: invoice_sn,
        invoice_pdf: invoice_pdf
      }
    }
    
    app._getNetWork({
      url: urlStr,
      data: json,
      success: ({
        data: res
      }) => {
        app.tips(res.msg);
        if (res.result == 1) {
          this.hideEditEmail();
        }
      }
    })
  },

  //预览图片，放大预览
  preview(e) {
    let currentUrl = e.currentTarget.dataset.src;
    wx.previewImage({
      current: currentUrl,
    });
  },

  downpdf(event) {
    let fileUrl = event.currentTarget.dataset.download_url;
    if (app._isTextEmpty(fileUrl)) {
      return app.tips("暂无发票！")
    }
    wx.downloadFile({
      url: fileUrl,
      success(res) {
        var filePath = res.tempFilePath;
        wx.openDocument({
          filePath,
          showMenu:true,
          success() {
            app.tips("文件打开成功");
          },
          fail() {
            app.tips("打开文件失败");
          }
        });
      },
      fail(err) {
        console.log(err, "是失败");
        wx.showModal({
          content: "下载失败，请重新下载"
        });
      }
    });
  },

  noop() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})